package com.xywy.bloodandroid.act;

import java.util.regex.Pattern;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.BaseHttpResponse;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.LogUtil;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.GetRequest;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;
/*
 * 忘记密码
 */
public class ForgotPasswordAct extends BaseActivity {
	private RelativeLayout login_button;
	private RelativeLayout left_img;
	private TextView middle_title;
	private TextView right_title;
	private ImageView right_img;
	private Button forgot_password_button;
	private EditText phonenumber;
	private int phone_flag;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_forgotprassword);
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		right_title = (TextView) findViewById(R.id.right_title);
		right_img = (ImageView) findViewById(R.id.right_img);
		middle_title.setText(getResources().getString(R.string.forgot_password));
		right_title.setVisibility(View.INVISIBLE);
		forgot_password_button = (Button) findViewById(R.id.forgot_password_button);
		phonenumber = (EditText)findViewById(R.id.phonenumber);
		chenck_phone_text = (TextView) findViewById(R.id.chenck_phone_text);
		chenck_phone_img = (ImageView) findViewById(R.id.chenck_phone_img);
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
		forgot_password_button.setOnClickListener(this);
		forgot_password_button.setClickable(false);
		
		phonenumber.addTextChangedListener(PhoneTextWatcher);
		phonenumber.setSelection(phonenumber.length());

	}

	@Override
	protected void initData() {

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;
		case R.id.forgot_password_button:
//			 if(PhoneNUmberIsNull())
				 SendForgetPasswordCode();
			
			break;
 
		default:
			break;
		}
	}
	private void SendForgetPasswordCode() {
		final String target=phonenumber.getText().toString().trim();
		GetRequest<BaseHttpResponse> getRequest=new GetRequest<BaseHttpResponse>(Constants.getUrl("sms", "sendCode")+"&target="+target+"&type=2", BaseHttpResponse.class, 
				new OnVolleyResponseListener<BaseHttpResponse>() {

					@Override
					public void onError(VolleyError volleyError) {
						UIUtil.DissDialog(context, waittingDialog);
						UIUtil.showToast(context, getResources().getString(R.string.network_slow));
					}

					@Override
					public void onSuccess(BaseHttpResponse response) {
						UIUtil.DissDialog(context, waittingDialog);
						 if(response.getCode()==10000){
							    LogUtil.i(context, response.getId());
							   Intent intent=new Intent(context, ComfrimCodeAct.class);
								intent.putExtra("flag", 1);
								intent.putExtra("target", target);
								intent.putExtra("id", response.getId());
								startActivity(intent);
						 }else{
							
							 UIUtil.showToast(context, response.getError());
						 }
					}
				});
		executeRequest(getRequest);
	}
	
	private boolean PhoneNUmberIsNull(){
		if(TextUtils.isEmpty(phonenumber.getText().toString().trim())){
			return false;
		}else
			return true;
	}
	
	private TextWatcher PhoneTextWatcher = new TextWatcher() {
		private int editEnd;
		private int editStart;
		private CharSequence temp;

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			temp = s;
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		@Override
		public void afterTextChanged(Editable s) {
			editStart = phonenumber.getSelectionStart();
			editEnd = phonenumber.getSelectionEnd();
			phonenumber.removeTextChangedListener(PhoneTextWatcher);
			if (Pattern.matches(Constants.PHONE_MATCH, temp)) {
				chenck_phone_img.setVisibility(View.VISIBLE);
				phone_flag = -1;
				if (phone_flag == -1 ) {
					forgot_password_button.setClickable(true);
					forgot_password_button
							.setBackgroundResource(R.drawable.login_button_selector);
				}

			} 
			if(!Pattern.matches(Constants.PHONE_MATCH, temp)) {
				phone_flag = 0;
				chenck_phone_img.setVisibility(View.GONE);
				forgot_password_button.setClickable(false);
				forgot_password_button
				.setBackgroundResource(R.drawable.login_button_press);
			}
			if (temp.length() != 0) {
				chenck_phone_text.setVisibility(View.GONE);
			} else {
				chenck_phone_text.setVisibility(View.VISIBLE);
			}

			phonenumber.setText(s);
			phonenumber.setSelection(editStart);

			phonenumber.addTextChangedListener(PhoneTextWatcher);
		}
	};

	private TextView chenck_phone_text;
	private ImageView chenck_phone_img;
	}
	
